package cn.ffcs.businessrequest.web;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.bpm.model.ProcTranRecord;
import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.bpm.model.Transactor;
import cn.ffcs.bpm.service.ProcTranRecordService;
import cn.ffcs.bpm.service.TransactProcessStatusService;
import cn.ffcs.bpm.service.TransactorService;
import cn.ffcs.bpm.web.BizProcessManageAction;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.businessrequest.model.ProdAdvReq;
import cn.ffcs.businessrequest.model.ReqDelay;
import cn.ffcs.businessrequest.model.ReqDocu;
import cn.ffcs.businessrequest.model.ReqProgress;
import cn.ffcs.businessrequest.service.BusinessRequestService;
import cn.ffcs.businessrequest.service.ProdAdvReqService;
import cn.ffcs.businessrequest.service.ReqDocuService;
import cn.ffcs.businessrequest.service.ReqProgressService;
import cn.ffcs.common.model.Attach;
import cn.ffcs.common.service.AttachService;
import cn.ffcs.product.model.ProductReference;
import cn.ffcs.security.model.User;
import cn.ffcs.share.model.ShareReference;
import cn.ffcs.system.dict.model.Dict;
import cn.ffcs.system.dict.model.DictType;
import cn.ffcs.util.context.ApplicationContextUtil;

/**
 * 
 * 业务需求交互单action层，实现页面业务逻辑<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class BusinessRequestAction<P extends BusinessRequest> extends
        BizProcessManageAction<P>
{
    private static final int BUFFER_SIZE = 512 * 1024;
    /** 业务POJO **/
    private P businessRequest;
    /** 业务POJO **/
    private String curUser;
    /** 业务POJO **/
    private TransactProcessStatus transactProcessStatus;
    /** 共享资料服务 **/
    private BusinessRequestService businessRequestService;
    /** 办理人服务 **/
    private TransactorService transactorService;
    /** 附件列表数据 **/
    private List<ReqDocu> reqDocus;
    /** 附件服务 **/
    private ReqDocuService reqDocuService;
    /** 服务 **/
    private ProdAdvReqService prodAdvReqService;
    /** 附件上传 **/
    private List<File> uploads;
    /** 上传附件名称 **/
    private List<String> fileNames;
    /** 上传附件名称 **/
    private List<String> uploadContentTypes;
    /** 办理人类型服务 **/
    private ProcTranRecordService procTranRecordService;
    /** 服务 **/
    private TransactProcessStatusService transactProcessStatusService;
    /** 流程步骤数据集 **/
    private List<ProcTranRecord> procTranRecords;
    private List<Transactor> transactors;
    /** 需求进展情况服务 **/
    private ReqProgressService<ReqProgress> reqProgressService;
    
    private List<ProdAdvReq> prodAdvReqs;
    
    
    /**
     * 
     * 初始化业务数据
     * @return
     */
    @SuppressWarnings("unchecked")
    public String init(){
        if (businessRequest!=null){
            Criterion cond1 = Restrictions.eq("refEntity", businessRequest.getClass().getName());
            Criterion cond2 = Restrictions.eq("refEntityId", businessRequest.getId().toString());
            Criterion condAttach = Restrictions.and(cond1, cond2);
            reqDocus=reqDocuService.findBy(condAttach);
            Criterion cond3 = Restrictions.eq("refEntityId", businessRequest);
            Criterion condProcTranRecord = Restrictions.and(cond1, cond3);
            procTranRecords=procTranRecordService.findByAndOrder(condProcTranRecord, "transactTime", "asc");
            
            Criterion cond5 = Restrictions.eq("relaReq", businessRequest);
            prodAdvReqs=prodAdvReqService.findBy(cond5);
        }
        return SUCCESS;
    }
    
    /**
     * 获取关键字获取实体
     * @return
     */  
    public String findById()
    {
        if (businessRequest != null && businessRequest.getId() != 0)
        {
            businessRequest = (P) baseService.findById(businessRequest.getId());
            
            Criterion cond = Restrictions.eq("refEntityId", businessRequest);
            List<TransactProcessStatus> list = transactProcessStatusService.findBy(cond);
            if(list!=null&&list.size()>0){
                transactProcessStatus=list.get(0);
                getTransactors(transactProcessStatus);
            }
        }
        curUser=ApplicationContextUtil.getUserContextObject().getCurrUser().getPerson().getName();
        //业务数据初始化
        init();
        //流程初始化
//        businessRequestService.initProcess(businessRequest, businessRequest.getId().toString());
        

        Criterion condt4 = Restrictions.eq("requestBusiness", businessRequest);
        //历史进展信息
        if(procTranRecords != null && procTranRecords.size() > 0){
            for (Iterator iterator = procTranRecords.iterator(); iterator.hasNext();)
            {
                ProcTranRecord temrp = (ProcTranRecord) iterator.next();
                //关联流程节点步骤
                Criterion cond5 = Restrictions.eq("processConfigDetail", temrp.getProcessStep());
                Criterion cond7 = Restrictions.eq("recotdId", temrp.getId().toString());
                Criterion condtemp = Restrictions.and(condt4, cond5);
                Criterion condtemp1 = Restrictions.and(condtemp, cond7);
                
                List<ReqProgress> tempes = reqProgressService.findBy(condtemp1);
                temrp.setReqProgresses(tempes);
            }
        }
        //历史延迟信息
        if(procTranRecords != null && procTranRecords.size() > 0){
            for (Iterator iterator = procTranRecords.iterator(); iterator.hasNext();)
            {
                ProcTranRecord temrp = (ProcTranRecord) iterator.next();
                //关联流程节点步骤
                Criterion cond5 = Restrictions.eq("processConfigDetail", temrp.getProcessStep());
                Criterion cond7 = Restrictions.eq("recotdId", temrp.getId().toString());
                Criterion condtemp = Restrictions.and(condt4, cond5);
                Criterion condtemp1 = Restrictions.and(condtemp, cond7);
                List<ReqDelay> tempes = reqProgressService.findByrd(condtemp1);
                temrp.setReqDelayes(tempes);
            }
        }
        
        return SUCCESS;
    }
    
    
    public void getTransactors(TransactProcessStatus transactProcessStatus){
        if(transactProcessStatus!=null&&transactProcessStatus.getId()!=null){
            Criterion cond1 = null;
            cond1 = Restrictions.eq("currProcess", transactProcessStatus);
            Criterion conds = Restrictions.and(cond1, Restrictions.eq("tranType", "1"));
            transactors=transactorService.findBy(conds);
        }
        return ;
    }

    /**
     * 
     * 组合查询条件
     * @return
     */
    public Criterion combineCond(){
        Criterion cond = null;
        if (super.getConds() != null)
        {
            //遍历参数表
            Iterator i = super.getConds().entrySet().iterator();
            while (i.hasNext())
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (super.getConds().get(key) != null
                        && !StringUtils.isEmpty(super.getConds().get(key).toString()))
                {
                    //根据实际情况做处理
                    Criterion temp=null;
                    if (key.equalsIgnoreCase("name")){
                        temp=Restrictions.like(key, "%"+super.getConds().get(key).toString()+"%");
                    }else{
                        temp=Restrictions.eq(key, super.getConds().get(key));                        
                    }
                    if (cond == null)
                    {
                        cond = temp;
                    }
                    else
                    {
                        cond = Restrictions.and(cond, temp);
                    }
                }
            }
        }
        return cond;
    }

    
    
    /**
     * 
     * 保存操作：
     *     保存业务和流程数据信息
     * @return
     */
    public String saveOrUpdate()
    {
        // 获取附件是否为发布
        String[] publishIDs = getContextObjects("publishIDs");
        String[] attachIDs = getContextObjects("attachIDs");
        // 删除附件
        String[] delIDs = getContextObjects("delIDs");
        // 保存产品资料数据
        businessRequestService.saveBusinessRequest(businessRequest,
                uploads, fileNames, publishIDs, attachIDs, delIDs);

        //初始化数据
        init();
        //流程初始化
//        businessRequestService.initProcess(businessRequest, businessRequest.getId().toString());
        return SUCCESS;
    }
    
    /**
     * 提交操作：
     *     流程提交，当前步骤信息修改
     * @return
     */
    public String submit(){
        businessRequestService.submit();
        //初始化数据
        init();
        //流程初始化
//        businessRequestService.initProcess(businessRequest, businessRequest.getId().toString());
        return SUCCESS;
    }
    
    /**
     * 
     * 删除操作
     * @return
     */
    public String remove()
    {
//        businessRequestService.remove(businessRequest);
        return SUCCESS;
    }
    
    public P getBusinessRequest()
    {
        return businessRequest;
    }
    public void setBusinessRequest(P businessRequest)
    {
        this.businessRequest = businessRequest;
    }
    public BusinessRequestService getBusinessRequestService()
    {
        return businessRequestService;
    }
    public void setBusinessRequestService(
            BusinessRequestService businessRequestService)
    {
        this.businessRequestService = businessRequestService;
    }

    public List<String> getFileNames()
    {
        return fileNames;
    }

    public void setFileNames(List<String> fileNames)
    {
        this.fileNames = fileNames;
    }

    public List<String> getUploadContentTypes()
    {
        return uploadContentTypes;
    }

    public void setUploadContentTypes(List<String> uploadContentTypes)
    {
        this.uploadContentTypes = uploadContentTypes;
    }

    public List<File> getUpload()
    {
        return uploads;
    }

    public void setUpload(List<File> uploads)
    {
        this.uploads = uploads;
    }
    
    public List<String> getUploadFileName()
    {
        return fileNames;
    }

    public void setUploadFileName(List<String> fileNames)
    {
        this.fileNames = fileNames;
    }

    public List<ProcTranRecord> getProcTranRecords()
    {
        return procTranRecords;
    }

    public void setProcTranRecords(List<ProcTranRecord> procTranRecords)
    {
        this.procTranRecords = procTranRecords;
    }

    public ProcTranRecordService getProcTranRecordService()
    {
        return procTranRecordService;
    }

    public void setProcTranRecordService(ProcTranRecordService procTranRecordService)
    {
        this.procTranRecordService = procTranRecordService;
    }

    public TransactProcessStatus getTransactProcessStatus()
    {
        return transactProcessStatus;
    }

    public void setTransactProcessStatus(TransactProcessStatus transactProcessStatus)
    {
        this.transactProcessStatus = transactProcessStatus;
    }

    public TransactProcessStatusService getTransactProcessStatusService()
    {
        return transactProcessStatusService;
    }

    public void setTransactProcessStatusService(
            TransactProcessStatusService transactProcessStatusService)
    {
        this.transactProcessStatusService = transactProcessStatusService;
    }

    public String getCurUser()
    {
        return curUser;
    }

    public void setCurUser(String curUser)
    {
        this.curUser = curUser;
    }

    public TransactorService getTransactorService()
    {
        return transactorService;
    }

    public void setTransactorService(TransactorService transactorService)
    {
        this.transactorService = transactorService;
    }

    public List<Transactor> getTransactors()
    {
        return transactors;
    }

    public void setTransactors(List<Transactor> transactors)
    {
        this.transactors = transactors;
    }

    public List<ReqDocu> getReqDocus()
    {
        return reqDocus;
    }

    public void setReqDocus(List<ReqDocu> reqDocus)
    {
        this.reqDocus = reqDocus;
    }

    public ReqDocuService getReqDocuService()
    {
        return reqDocuService;
    }

    public void setReqDocuService(ReqDocuService reqDocuService)
    {
        this.reqDocuService = reqDocuService;
    }

    public ReqProgressService<ReqProgress> getReqProgressService()
    {
        return reqProgressService;
    }

    public void setReqProgressService(
            ReqProgressService<ReqProgress> reqProgressService)
    {
        this.reqProgressService = reqProgressService;
    }

    public ProdAdvReqService getProdAdvReqService()
    {
        return prodAdvReqService;
    }

    public void setProdAdvReqService(ProdAdvReqService prodAdvReqService)
    {
        this.prodAdvReqService = prodAdvReqService;
    }

    public List<ProdAdvReq> getProdAdvReqs()
    {
        return prodAdvReqs;
    }

    public void setProdAdvReqs(List<ProdAdvReq> prodAdvReqs)
    {
        this.prodAdvReqs = prodAdvReqs;
    }



}
